package cn.mazexal.examapi.controller;

import cn.mazexal.examapi.common.anno.PermitGuest;
import cn.mazexal.examapi.common.models.BizException;
import cn.mazexal.examapi.common.models.CustomerError;
import cn.mazexal.examapi.common.models.MultiResponse;
import cn.mazexal.examapi.common.models.SingleResponse;
import cn.mazexal.examapi.models.bo.AliyunLoginResBO;
import cn.mazexal.examapi.models.bo.Credentials;
import cn.mazexal.examapi.models.enums.PlatformEnum;
import cn.mazexal.examapi.models.enums.RequestHeaderEnum;
import cn.mazexal.examapi.models.form.AliyunLoginForm;
import cn.mazexal.examapi.models.qry.ExamBankIdQry;
import cn.mazexal.examapi.models.qry.IDQry;
import cn.mazexal.examapi.models.qry.UserMsgQry;
import cn.mazexal.examapi.models.vo.*;
import cn.mazexal.examapi.services.UserMsgService;
import cn.mazexal.examapi.services.UserService;
import cn.mazexal.examapi.services.UserVipService;
import cn.mazexal.examapi.utils.AliyunLoginUtils;
import cn.mazexal.examapi.utils.JWTProvider;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author: Mazexal
 * Date: 2025/5/9
 */
@RestController
@RequestMapping("/user")
@Tag(name = "user", description = "用户接口")
public class UserController extends BaseController {


    @Resource
    private AliyunLoginUtils aliyunLoginUtils;

    @Resource
    private HttpServletRequest request;


    @Resource
    private UserService userService;

    @Resource
    private UserMsgService userMsgService;
    @Autowired
    private UserVipService userVipService;

    @PostMapping("/login")
    @PermitGuest
    public SingleResponse<UserVO> userLogin(@RequestBody IDQry id) {
        UserVO userVO = new UserVO();
        userVO.setUserName("mazexal");
        userVO.setMobile("13800000001");
        String platform = request.getHeader(RequestHeaderEnum.PLATFORM.getValue());
        String encode = userService.getUserToken(id.getId(), platform);
        userVO.setToken(encode);
        userVO.setId(id.getId());
        return SingleResponse.of(userVO);
    }

    @PostMapping("/getUserVipExpireTime")
    @Schema(description = "获取用户VIP到期时间,传入examBankId")
    public SingleResponse<UserVipResSingleVO> getUserVipExpireTime(@RequestBody ExamBankIdQry id) {
        Long userId = getUserId();
        Long expireTime = userVipService.getUserVipExpireTime(userId, id.getExamBankId());
        UserVipResSingleVO resVO = new UserVipResSingleVO();
        resVO.setExpireTime(expireTime);
        resVO.setCurrentTime(System.currentTimeMillis());
        return SingleResponse.of(resVO);
    }

    @PostMapping("/getUserVipExpireTimeList")
    @Schema(description = "获取用户所有VIP到期时间")
    public SingleResponse<UserVipResVO> getUserVipExpireTimeList() {
        Long userId = getUserId();
        List<UserVipResItemVO> userVipExpireTimeList = userVipService.getUserVipExpireTimeList(userId);
        UserVipResVO resVO = new UserVipResVO();
        resVO.setUserVipList(userVipExpireTimeList);
        resVO.setCurrentTime(System.currentTimeMillis());
        return SingleResponse.of(resVO);
    }

    /**
     * 阿里云融合登录获取token
     */
    @PermitGuest
    @PostMapping("/aliyun/login")
    @Schema(description = "阿里云融合登录获取token")
    public SingleResponse<String> getAliyunFusionAuthToken() throws Exception {
        String platform = request.getHeader(RequestHeaderEnum.PLATFORM.getValue());
        PlatformEnum platformEnum = PlatformEnum.getByValue(platform);
        // 这里要转换一下
        platformEnum = transformPlatform(platformEnum);
        String token = aliyunLoginUtils.getFusionAuthTokenRequest(platformEnum);
        return SingleResponse.of(token);
    }

    PlatformEnum transformPlatform(PlatformEnum platformEnum) {
        if (platformEnum.equals(PlatformEnum.IOS) || platformEnum.equals(PlatformEnum.iPadOS)) {
            return PlatformEnum.IOS;
        }
        if (platformEnum.equals(PlatformEnum.ANDROID) || platformEnum.equals(PlatformEnum.AndroidPad)) {
            return PlatformEnum.ANDROID;
        }
        throw new BizException(new CustomerError("500", "不支持的平台"));
    }

    @PermitGuest
    @PostMapping("/aliyun/verify")
    @Schema(description = "阿里云融合登录验证")
    public SingleResponse<UserVO> verifyToken(@RequestBody AliyunLoginForm aliyunLoginForm) throws Exception {
        AliyunLoginResBO res = aliyunLoginUtils.verifyToken(aliyunLoginForm.getAuthToken());
        String platform = request.getHeader(RequestHeaderEnum.PLATFORM.getValue());
        if (res.getRes()) {
            UserVO userVO = userService.userLoginOrRegisterByMobile(res.getMobile());
            userVO.setToken(userService.getUserToken(userVO.getId(), platform));
            return SingleResponse.of(userVO);
        } else {
            return SingleResponse.error("500", "阿里云登录校验失败, 请稍后重试或更换登录方式");
        }

    }

    @PostMapping("/msg/list")
    @Schema(description = "获取用户消息列表")
    public MultiResponse<UserMsgVO> getUserMsgList(@RequestBody UserMsgQry qry) {
        return MultiResponse.of(userMsgService.getUserMsgVOList(getUserId(), qry.getLastId(), qry.getPageSize()));
    }

    @PostMapping("/msg/read")
    @Schema(description = "标记已读用户消息")
    public SingleResponse<Boolean> readMsg(@RequestBody IDQry id) {
        return SingleResponse.of(userMsgService.readMsg(id.getId()));
    }

    @PostMapping("/msg/del")
    @Schema(description = "删除用户消息")
    public SingleResponse<Boolean> delMsg(@RequestBody IDQry id) {
        return SingleResponse.of(userMsgService.delMsg(id.getId()));
    }
}
